<%@ page import="com.tm.controller.InstrumentController" %>
<%@ page import="com.tm.pojo.Instrument" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>

<jsp:include page="header.jsp"></jsp:include>

<%
boolean isAdmin = false;
UserService userService = UserServiceFactory.getUserService();
if(userService.isUserLoggedIn() && userService.getCurrentUser().getNickname().equals("vahe@andonians.com")) {
	isAdmin = true;
}
InstrumentController ic = InstrumentController.getInstance();
Iterator<Instrument> instruments = ic.getInstruments().iterator();
%>

<%
if (isAdmin) {
	%>
	<script type="text/javascript" src="instruments.js"></script>
	<form action="">
		<table border="0" class="form-table" >
			<tr>
				<td class="form-label-cell">Term</td>
				<td class="form-input-cell"><input name="term" id="term" type="text" size="10" value="" class="text-input">&nbsp;use w, m or y suffix (eg 3w)</td>
				<td class="form-error"><label class="error" for="term" id="term_error">This field is required.</label></td>
			</tr>
			<tr>
				<td class="form-label-cell">Name</td>
				<td class="form-input-cell"><input name="name" id="name" type="text" size="20" value="" class="text-input"></td>
				<td class="form-error"><label class="error" for="name" id="name_error">This field is required.</label></td>
			</tr>
			<tr>
				<td class="form-label-cell">Type</td>
				<td class="form-input-cell"><input name="type" id="type" type="text" size="20" value="" class="text-input">&nbsp;cash/swap/...</td>
				<td class="form-error"><label class="error" for="type" id="type_error">This field is required.</label></td>
			</tr>
			<tr>
				<td class="form-label-cell">Bloomberg ID</td>
				<td class="form-input-cell"><input name="bloombergId" id="bloombergId" type="text" size="20" value="" class="text-input"></td>
				<td class="form-error"></td>
			</tr>
			<tr>
				<td class="form-label-cell">Definition</td>
				<td class="form-input-cell"><input name="definition" id="definition" size="60" class="text-input"></td>
				<td class="form-error"></td>
			</tr>
			<tr>
				<td class="form-label-cell">Currency</td>
				<td class="form-input-cell"><input name="currency" id="currency" type="text" size="10" value="" class="text-input"></td>
				<td class="form-error"><label class="error" for="currency" id="currency_error">This field is required.</label></td>
			</tr>
			<tr>
				<td class="form-label-cell">Daycount Convention</td>
				<td class="form-input-cell"><input name="daycountConvention" id="daycountConvention" type="text" size="10" value="" class="text-input"></td>
				<td class="form-error"><label class="error" for="daycountConvention" id="daycountConvention_error">This field is required.</label></td>
			</tr>
			<tr>
				<td class="form-label-cell">Compounding Frequency</td>
				<td class="form-input-cell"><input name="compoundingFrequency" id="compoundingFrequency" type="text" size="10" value="" class="text-input">&nbsp;n-times per period</td>
				<td class="form-error"><label class="error" for="compoundingFrequency" id="compoundingFrequency_error">This field is required.</label></td>
			</tr>
			<tr>
				<td class="form-label-cell">&nbsp;</td>
				<td class="form-input-cell"><input type="submit" name="submit" class="button new-button" id="submit_btn" value="Save" /></td>
				<td class="form-error"></td>
			</tr>
		</table>
	</form>
	<p></p>
<%
}
%>

<table border="0" id="tblInstruments">
	<tr><th>Id</th><th>Term in days</th><th>Term</th><th>Name</th><th>Type</th><th>Bloomberg Id</th><th>Definition</th><th>Currency</th><th>Daycount Convention</th><th>Compounding Frequency</th><th>Action</th></tr>
	<%
	while(instruments.hasNext()) {
		Instrument i = instruments.next();
	%>
		<tr id="row<%=i.getInstrumentId()%>">
			<td><%=i.getInstrumentId()%></td>
			<td><%=i.getTerm()%></td>
			<td><%=i.getStrTerm()%></td>
			<td><%=i.getName()%></td>
			<td><%=i.getType()%></td>
			<td><%=i.getBloombergId()%></td>
			<td><%=i.getDefinition()%></td>
			<td><%=i.getCurrency()%></td>
			<td><%=i.getDaycountConvention()%></td>
			<td><%=i.getCompoundingFrequency()%></td>
			<td>
				<%
				if (isAdmin) { 
				%>
					<button type="button" id="btn<%=i.getInstrumentId()%>" class="button delete-button" onClick="deleteInstrument(<%=i.getInstrumentId()%>)">Delete</button>
				<%
				} else {
					%>Admin required<%	
				}
				%>
			</td>
		</tr>
	<%
	}
	%>
</table>

<jsp:include page="footer.jsp"></jsp:include>